<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜品名称" prop="cuisineName">
        <el-input
          v-model="queryParams.cuisineName"
          placeholder="请输入菜品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品描述" prop="cuisineDescribe">
        <el-input
          v-model="queryParams.cuisineDescribe"
          placeholder="请输入菜品描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品金额" prop="cuisineMoney">
        <el-input
          v-model="queryParams.cuisineMoney"
          placeholder="请输入菜品金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户id" prop="commercialId">
        <el-input
          v-model="queryParams.commercialId"
          placeholder="请输入商户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['variety:cuisine:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['variety:cuisine:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['variety:cuisine:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['variety:cuisine:export']"
        >导出</el-button>
        {{url}}
      </el-col>
      <el-col :span="1.5">
<!--        <el-button type="primary"  plain
                   icon="el-icon-download"
                   size="mini">导入</el-button>-->
        <el-upload
          class="upload-demo"
          action="http://localhost:18580/cuisine/dr1"
          :multiple="true"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          name="files"
          multiple
          :limit="3"
          :on-exceed="handleExceed"
          >
          <el-button size="small" type="primary">导入</el-button>
<!--          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
        </el-upload>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cuisineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜品id" align="center" prop="cuisineId" />
      <el-table-column label="菜品名称" align="center" prop="cuisineName" />
      <el-table-column label="菜品描述" align="center" prop="cuisineDescribe" />
      <el-table-column label="菜品金额" align="center" prop="cuisineMoney" />
      <el-table-column label="菜品图片" align="center" prop="cuisineImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cuisineImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="菜品类型" align="center" prop="cuisineType" >
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.cuisineType==1">肉类</el-button>
          <el-button type="success" v-if="scope.row.cuisineType==2">素食</el-button>
        </template>
      </el-table-column>
      <el-table-column label="商户id" align="center" prop="commercialId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['variety:cuisine:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['variety:cuisine:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      {{form}}
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜品名称" prop="cuisineName">
          <el-input v-model="form.cuisineName" placeholder="请输入菜品名称" />
        </el-form-item>
        <el-form-item label="菜品描述" prop="cuisineDescribe">
          <el-input v-model="form.cuisineDescribe" placeholder="请输入菜品描述" />
        </el-form-item>
        <el-form-item label="菜品金额" prop="cuisineMoney">
          <el-input v-model="form.cuisineMoney" placeholder="请输入菜品金额" />
        </el-form-item>
<!--        <el-form-item label="菜品图片">
          <image-upload v-model="form.cuisineImage"/>
        </el-form-item>-->
        <el-upload
          class="avatar-uploader"
          action="http://localhost:18580/cuisine/oss"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

        <el-form-item label="菜品类型">
          <el-select v-model="form.cuisineType" placeholder="请选择活动区域">
            <el-option label="肉类" value="1"></el-option>
            <el-option label="素食" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户id" prop="commercialId">
          <el-input v-model="form.commercialId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="所属优惠卷">
          <span class="demonstration">hover 触发子菜单</span>
          <el-cascader
            v-model="preferentialList"
            :options="options"
            :props="props"
            @change="handleChange"></el-cascader>
<!--          <el-select v-model="form.preferentialId" placeholder="请选择活动区域">
            <el-option label="全场菜品满减" value="1"></el-option>
            <el-option label="部分菜品满减" value="2"></el-option>
          </el-select>-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {type,url,dc, listCuisine, getCuisine, delCuisine, addCuisine, updateCuisine } from "@/api/variety/cuisine";

export default {
  name: "Cuisine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜品表格数据
      cuisineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cuisineName: null,
        cuisineDescribe: null,
        cuisineMoney: null,
        cuisineImage: null,
        cuisineType: null,
        commercialId: null
      },
      // 表单参数
      form: {
        preferentialId:'',
        cuisineImage:''
      },
      imageUrl: '',
      // 表单校验
      rules: {
      },
      timeMessage:'',
      payLeftTimeSecond:1800,
      timer:null,
      url:'',
      options:[],
      props:{
        label:"pname",
        value:"pid"
      },
      preferentialList:[]

    };
  },
  created() {
    this.getList();
    this.findPage();
  },
  methods: {
    handleChange(){
        this.preferentialList.forEach(forEach =>{
          if(forEach!=null){
            console.log(forEach);
            this.form.preferentialId=forEach;
          }
        })
    },
    findPage(){
      type().then((resp)=>{
        console.log(resp);
        this.options=resp;
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(files) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    /** 查询菜品列表 */
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.imageUrl = res;
    },
    beforeAvatarUpload(file) {

      const isLt2M = file.size / 1024 / 1024 < 2;

      return ;
    },
    getList() {
      this.loading = true;
      listCuisine(this.queryParams).then(response => {
        this.cuisineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cuisineId: null,
        cuisineName: null,
        cuisineDescribe: null,
        cuisineMoney: null,
        cuisineImage: null,
        cuisineType: null,
        commercialId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cuisineId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.imageUrl=row.cuisineImage;
      this.reset();
      const cuisineId = row.cuisineId || this.ids
      getCuisine(cuisineId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.cuisineImage=this.imageUrl;
        if (valid) {
          if (this.form.cuisineId != null) {
            updateCuisine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCuisine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cuisineIds = row.cuisineId || this.ids;
      this.$modal.confirm('是否确认删除菜品编号为"' + cuisineIds + '"的数据项？').then(function() {
        return delCuisine(cuisineIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
    /*  this.download('variety/cuisine/export', {
        ...this.queryParams
      }, `cuisine_${new Date().getTime()}.xlsx`)*/
      dc(this.queryParams).then((resp)=>{
        this.timer = setInterval(this.opTimeOut,1000);//1 setInterval 设置定时器

      })
    },
    opTimeOut(){//定时方法
      this.payLeftTimeSecond --;
      if(this.payLeftTimeSecond % 5 ==0){
         url().then((resp)=>{
           this.url=resp;
           if(resp!=null){
             this.payLeftTimeSecond=0;
           }
           console.log(resp);
         })
      }
    },
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

